package online3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class CalcClient {
    private DatagramSocket socket=null;
    private String serverIp;
    private int serverPort;
    public CalcClient(String serverIp,int serverPort) throws SocketException {
        this.serverIp=serverIp;
        this.serverPort=serverPort;
        this.socket=new DatagramSocket();
    }
    public void start() throws IOException {
        Scanner scanner=new Scanner(System.in);
        while (true){
            System.out.println("请输入操作数num1:");
            int num1=scanner.nextInt();
            System.out.println("请输入操作数num2:");
            int num2=scanner.nextInt();
            System.out.println("请输入运算符(+ - * /):");
            String operator=scanner.next();
            String request=num1+";"+num2+";"+operator;
            DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length, InetAddress.getByName(serverIp),
                    serverPort);
            socket.send(requestPacket);
            DatagramPacket responsePacket=new DatagramPacket(new byte[4096],4096);
            socket.receive(responsePacket);
            String response=new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.println("计算结果："+response);
        }
    }

    public static void main(String[] args) throws IOException {
        CalcClient client=new CalcClient("127.0.0.1",9090);
        client.start();
    }
}